package com.mew.slots.core.game.mnyys.service;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.mew.common.enm.SlotsWinType;
import com.mew.common.entity.game.WinFree;
import com.mew.common.exception.ResponseCode;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.mnyys.dto.Mg;
import com.mew.slots.core.game.mnyys.dto.MnyysInRoom;
import com.mew.slots.core.game.mnyys.message.MnyysBonusPlayResponseMessage;
import com.mew.slots.core.game.mnyys.room.MgUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author dylin
 * @create 2021/1/29 0029
 */
@Service
public class MnyysBonusService {
    private final int[] tierNums={5,5,4,3,2};
    public MnyysBonusPlayResponseMessage play(StrategyGameVO player){
        MnyysBonusPlayResponseMessage message=new MnyysBonusPlayResponseMessage();
        WinFree winFree=player.getWinFree();
        if(winFree.getBounsNumber()<1){
            player.setStat(ResponseCode.BounsCanNotPlay.code());
            return message;
        }
        if(player.getOtherMsg()==null){
            //参数错误
            player.setStat(ResponseCode.ParamInvalid.code());
            return message;
        }
        MnyysInRoom vo = JSON.parseObject(winFree.getJson(), MnyysInRoom.class);
        if(vo.getMgs().size()==0){
            MgUtil.initMgs(vo.getMgs(),winFree.getBet()*winFree.getLastLineNumber());
        }
        int[] courses=null;
        for (int[] cours : vo.getProgressBar()) {
            if(cours[0]==(int)winFree.getBet().longValue()){
                courses=cours;
                break;
            }
        }
        courses[1]=0;
        int open=player.getOtherMsg()[0];
        int openTire=0;
        int temp=open;
        for(int i:tierNums){
            temp=open-i;
            if(temp>0){
                openTire++;
            }else{
                break;
            }
        }
        if(openTire!=vo.getTier()&&vo.getOpens().contains(open)){
            player.setStat(ResponseCode.BounsParamInvalid.code());
            return message;
        }

        playGame(player,open,message,vo);
        player.setBetNumber(0);
        player.setInBetNumber(winFree.getBet());
        player.setBetTimes((int)(message.getGold()/winFree.getBet()/winFree.getLastLineNumber()));

        if(message.getIsGameOver()==1){
            player.setPaymentNumber(message.getAllGold());
            //重置json
            winFree.setBounsNumber(0);
        }else{
            player.setPaymentNumber(0);
        }
        player.iconResult(new int[]{
             open,message.getUp(),(int)message.getGold()
        });
        winFree.setJson(JSON.toJSONString(vo));
        return message;
    }
    private void playGame(StrategyGameVO player,int open,MnyysBonusPlayResponseMessage message, MnyysInRoom vo){
        player.setWinType(SlotsWinType.Special.code());
        int tier=vo.getTier();
        int ei=vo.getStartIndex()+tierNums[tier];

        Mg mg=vo.getMgs().get(open);
        message.setUp(mg.getUp());
        message.setGold(mg.getGold());
        message.setAllGold(vo.getAllGold()+message.getGold());
        message.setMgs(new ArrayList<>(vo.getMgs().subList(vo.getStartIndex(),ei)));
        vo.getOpens().add(open);
        tier++;
        if(mg.getUp()==0){
            message.setIsGameOver(1);
            message.setAllMgs(vo.getMgs());
            clearVo(vo);
        }else if(tier==5) {
            message.setJpNo(1);
            message.setIsGameOver(1);
            message.setAllMgs(vo.getMgs());
            clearVo(vo);
            player.setWinType(SlotsWinType.Jackpot.code());
        }else{
            vo.setTier(tier);
            vo.setAllGold(message.getAllGold());
            vo.setStartIndex(ei);
        }

    }
    private void clearVo(MnyysInRoom vo){
        vo.setTier(0);
        vo.setStartIndex(0);
        vo.setAllGold(0);
        vo.setMgs(new ArrayList<>());
        vo.setOpens(new ArrayList<>());
    }

    public static void main(String[] args) {
        long allwin=0L;
        for (int i = 0; i < 10000; i++) {
            MnyysInRoom vo=new MnyysInRoom();
            StrategyGameVO player=new StrategyGameVO();
            WinFree winFree=new WinFree();
            winFree.setJson(JSON.toJSONString(vo));
            winFree.setBet(1000L);
            winFree.setLastLineNumber(80);
            winFree.setBounsNumber(1);
            player.setWinFree(winFree);
            MnyysBonusService bonusService=new MnyysBonusService();
            MnyysBonusPlayResponseMessage message;
            int[] opens={RandomUtil.randomInt(0,5),RandomUtil.randomInt(5,10),RandomUtil.randomInt(10,14),RandomUtil.randomInt(14,17),RandomUtil.randomInt(17,19)};
            int open=0;
            do{
                player.setOtherMsg(new int[]{opens[open]});
                message=bonusService.play(player);
                open++;
            }while (message.getIsGameOver()==0);
            allwin+=message.getAllGold();
        }
        System.out.println(allwin);
        System.out.println(allwin/10000/80000.00);
    }
}
